import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class AppService extends BaseService<any> {
  private readonly logger = new Logger(AppService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * setConfig
   * 对应 PHP: AppService_admin::setConfig()
   * 逻辑类型: undefined - undefined
   */
  async setConfig(value: any[]) {
    // 基于PHP真实逻辑: setConfig
    // PHP原文: return (new CoreAppService())->setConfig($this->site_id, $value);     }      /**      * 获取app配置      * @return mixed      */     public function getCo...
return this.coreAppService.setConfig(this.site_id, value);
    }

    /**
     * 获取app配置
     * @return mixed
     */
    async getConfig(){
        return this.coreAppService.getConfig(this.site_id);
    }

    /**
     * @param where
     * @return * @throws \think\db\exception\DbBusinessException
     */
    async getVersionPage(where = [))
    {
        const order = 'id desc';
        const search_model = this.model.where([ [ 'site_id' ,"=", this.site_id ) )).withSearch(.platform, where).append(['platform_name', 'status_name')).field("*").order(order];
        const list = this.pageQuery(search_model);
        return list;
    }

    /**
     * @param id
     * @return AppVersion|array|mixed|\think\Model
     */
    async getVersionInfo(id) {
        return this.model.where([ [ 'site_id' ,"=", this.site_id ], ['id', '=', id) )).findOrEmpty().toArray(];
    }

    /**
     * 添加版本
     * @param data
     * @return mixed
     */
    async addVersion(data) {
        const not_release = this.model.where([['site_id', '=', this.site_id], ['release_time', '=', 0))).findOrEmpty(];
        if (!not_release.isEmpty()) throw new BusinessException("当前已存在未发布的版本"];

        const last_version = this.model.where([['site_id', '=', this.site_id))).order('id desc').findOrEmpty(];
        if (!last_version.isEmpty() && data.version_code <= last_version.version_code) throw new BusinessException("版本号必须高于上一版本设置的值"];

        data.site_id = this.site_id;

        const model = [
            site_id: this.site_id,
            version_code: data.version_code,
            version_name: data.version_name,
            version_desc: data.version_desc,
            platform: data.platform,
            is_forced_upgrade: data.is_forced_upgrade,
            package_path: data.package_path,
            upgrade_type: data.upgrade_type,
        );

        if (data.package_type == 'cloud') {
            const task_key = this.coreAppCloudService.appCloudBuid(data);
            model.task_key = task_key.key;
            model.status = AppDict.STATUS_CREATING;
  }

  /**
   * getConfig
   * 对应 PHP: AppService_admin::getConfig()
   * 逻辑类型: undefined - undefined
   */
  async getConfig() {
    // 基于PHP真实逻辑: getConfig
    // PHP原文: return (new CoreAppService())->getConfig($this->site_id);     }      /**      * @param array $where      * @return array      * @throws \think\db\exce...
return this.coreAppService.getConfig(this.site_id);
    }

    /**
     * @param where
     * @return * @throws \think\db\exception\DbBusinessException
     */
    async getVersionPage(where = [))
    {
        const order = 'id desc';
        const search_model = this.model.where([ [ 'site_id' ,"=", this.site_id ) )).withSearch(.platform, where).append(['platform_name', 'status_name')).field("*").order(order];
        const list = this.pageQuery(search_model);
        return list;
    }

    /**
     * @param id
     * @return AppVersion|array|mixed|\think\Model
     */
    async getVersionInfo(id) {
        return this.model.where([ [ 'site_id' ,"=", this.site_id ], ['id', '=', id) )).findOrEmpty().toArray(];
    }

    /**
     * 添加版本
     * @param data
     * @return mixed
     */
    async addVersion(data) {
        const not_release = this.model.where([['site_id', '=', this.site_id], ['release_time', '=', 0))).findOrEmpty(];
        if (!not_release.isEmpty()) throw new BusinessException("当前已存在未发布的版本"];

        const last_version = this.model.where([['site_id', '=', this.site_id))).order('id desc').findOrEmpty(];
        if (!last_version.isEmpty() && data.version_code <= last_version.version_code) throw new BusinessException("版本号必须高于上一版本设置的值"];

        data.site_id = this.site_id;

        const model = [
            site_id: this.site_id,
            version_code: data.version_code,
            version_name: data.version_name,
            version_desc: data.version_desc,
            platform: data.platform,
            is_forced_upgrade: data.is_forced_upgrade,
            package_path: data.package_path,
            upgrade_type: data.upgrade_type,
        );

        if (data.package_type == 'cloud') {
            const task_key = this.coreAppCloudService.appCloudBuid(data);
            model.task_key = task_key.key;
            model.status = AppDict.STATUS_CREATING;
  }

  /**
   * getVersionPage
   * 对应 PHP: AppService_admin::getVersionPage()
   * 逻辑类型: undefined - undefined
   */
  async getVersionPage(where: any[]) {
    // 基于PHP真实逻辑: getVersionPage
    // PHP原文: $order = 'id desc';         $search_model = $this->model->where([ [ 'site_id' ,"=", $this->site_id ] ])->withSearch(["platform"], $where)->append(['pl...
const order = 'id desc';
        const search_model = this.model.where([ [ 'site_id' ,"=", this.site_id ) )).withSearch(.platform, where).append(['platform_name', 'status_name')).field("*").order(order];
        const list = this.pageQuery(search_model);
        return list;
    }

    /**
     * @param id
     * @return AppVersion|array|mixed|\think\Model
     */
    async getVersionInfo(id) {
        return this.model.where([ [ 'site_id' ,"=", this.site_id ], ['id', '=', id) )).findOrEmpty().toArray(];
    }

    /**
     * 添加版本
     * @param data
     * @return mixed
     */
    async addVersion(data) {
        const not_release = this.model.where([['site_id', '=', this.site_id], ['release_time', '=', 0))).findOrEmpty(];
        if (!not_release.isEmpty()) throw new BusinessException("当前已存在未发布的版本"];

        const last_version = this.model.where([['site_id', '=', this.site_id))).order('id desc').findOrEmpty(];
        if (!last_version.isEmpty() && data.version_code <= last_version.version_code) throw new BusinessException("版本号必须高于上一版本设置的值"];

        data.site_id = this.site_id;

        const model = [
            site_id: this.site_id,
            version_code: data.version_code,
            version_name: data.version_name,
            version_desc: data.version_desc,
            platform: data.platform,
            is_forced_upgrade: data.is_forced_upgrade,
            package_path: data.package_path,
            upgrade_type: data.upgrade_type,
        );

        if (data.package_type == 'cloud') {
            const task_key = this.coreAppCloudService.appCloudBuid(data);
            model.task_key = task_key.key;
            model.status = AppDict.STATUS_CREATING;
  }

  /**
   * getVersionInfo
   * 对应 PHP: AppService_admin::getVersionInfo()
   * 逻辑类型: undefined - undefined
   */
  async getVersionInfo(id: any) {
    // 基于PHP真实逻辑: getVersionInfo
    // PHP原文: return $this->model->where([ [ 'site_id' ,"=", $this->site_id ], ['id', '=', $id] ])->findOrEmpty()->toArray();     }      /**      * 添加版本      * @par...
return this.model.where([ [ 'site_id' ,"=", this.site_id ], ['id', '=', id) )).findOrEmpty().toArray(];
    }

    /**
     * 添加版本
     * @param data
     * @return mixed
     */
    async addVersion(data) {
        const not_release = this.model.where([['site_id', '=', this.site_id], ['release_time', '=', 0))).findOrEmpty(];
        if (!not_release.isEmpty()) throw new BusinessException("当前已存在未发布的版本"];

        const last_version = this.model.where([['site_id', '=', this.site_id))).order('id desc').findOrEmpty(];
        if (!last_version.isEmpty() && data.version_code <= last_version.version_code) throw new BusinessException("版本号必须高于上一版本设置的值"];

        data.site_id = this.site_id;

        const model = [
            site_id: this.site_id,
            version_code: data.version_code,
            version_name: data.version_name,
            version_desc: data.version_desc,
            platform: data.platform,
            is_forced_upgrade: data.is_forced_upgrade,
            package_path: data.package_path,
            upgrade_type: data.upgrade_type,
        );

        if (data.package_type == 'cloud') {
            const task_key = this.coreAppCloudService.appCloudBuid(data);
            model.task_key = task_key.key;
            model.status = AppDict.STATUS_CREATING;
  }

  /**
   * addVersion
   * 对应 PHP: AppService_admin::addVersion()
   * 逻辑类型: undefined - undefined
   */
  async addVersion(data: any[]) {
    // 基于PHP真实逻辑: addVersion
    // PHP原文: $not_release = $this->model->where([['site_id', '=', $this->site_id], ['release_time', '=', 0]])->findOrEmpty();         if (!$not_release->isEmpty())...
const not_release = this.model.where([['site_id', '=', this.site_id], ['release_time', '=', 0))).findOrEmpty(];
        if (!not_release.isEmpty()) throw new BusinessException("当前已存在未发布的版本"];

        const last_version = this.model.where([['site_id', '=', this.site_id))).order('id desc').findOrEmpty(];
        if (!last_version.isEmpty() && data.version_code <= last_version.version_code) throw new BusinessException("版本号必须高于上一版本设置的值"];

        data.site_id = this.site_id;

        const model = [
            site_id: this.site_id,
            version_code: data.version_code,
            version_name: data.version_name,
            version_desc: data.version_desc,
            platform: data.platform,
            is_forced_upgrade: data.is_forced_upgrade,
            package_path: data.package_path,
            upgrade_type: data.upgrade_type,
        );

        if (data.package_type == 'cloud') {
            const task_key = this.coreAppCloudService.appCloudBuid(data);
            model.task_key = task_key.key;
            model.status = AppDict.STATUS_CREATING;
  }

  /**
   * editVersion
   * 对应 PHP: AppService_admin::editVersion()
   * 逻辑类型: undefined - undefined
   */
  async editVersion(id: any[], data: any[]) {
    // 基于PHP真实逻辑: editVersion
    // PHP原文: $last_version = $this->model->where([['site_id', '=', $this->site_id], ['id', '<>', $id]])->order('id desc')->findOrEmpty();         if (!$last_versio...
const last_version = this.model.where([['site_id', '=', this.site_id], ['id', '<>', id))).order('id desc').findOrEmpty(];
        if (!last_version.isEmpty() && data.version_code <= last_version.version_code) throw new BusinessException("版本号必须高于上一版本设置的值"];

        data.site_id = this.site_id;

        const model = [
            version_code: data.version_code,
            version_name: data.version_name,
            version_desc: data.version_desc,
            platform: data.platform,
            is_forced_upgrade: data.is_forced_upgrade,
            package_path: data.package_path,
            upgrade_type: data.upgrade_type,
        );

        if (data.package_type == 'cloud') {
            const task_key = this.coreAppCloudService.appCloudBuid(data);
            model.task_key = task_key.key;
            model.status = AppDict.STATUS_CREATING;
  }

  /**
   * delVersion
   * 对应 PHP: AppService_admin::delVersion()
   * 逻辑类型: undefined - undefined
   */
  async delVersion(id: number) {
    // 基于PHP真实逻辑: delVersion
    // PHP原文: $model = $this->model->where([['id', '=', $id],['site_id', '=', $this->site_id]])->find();         $res = $model->delete();         return $res;     }...
const model = this.model.where([['id', '=', id],['site_id', '=', this.site_id))).find(];
        const res = model.delete();
        return res;
    }

    async getBuildLog(string key) {
        const result = this.coreAppCloudService.getAppCompileLog(key);
        if (result.status == 'fail') {
            this.model.update([status: AppDict.STATUS_CREATE_FAIL, fail_reason: result.fail_reason, update_time: time() ], [task_key: key, site_id: this.site_id]];
  }

  /**
   * getBuildLog
   * 对应 PHP: AppService_admin::getBuildLog()
   * 逻辑类型: undefined - undefined
   */
  async getBuildLog(key: string) {
    // 基于PHP真实逻辑: getBuildLog
    // PHP原文: $result = (new CoreAppCloudService())->getAppCompileLog($key);         if ($result['status'] == 'fail') {             $this->model->update(['status' =...
const result = this.coreAppCloudService.getAppCompileLog(key);
        if (result.status == 'fail') {
            this.model.update([status: AppDict.STATUS_CREATE_FAIL, fail_reason: result.fail_reason, update_time: time() ], [task_key: key, site_id: this.site_id]];
  }

  /**
   * release
   * 对应 PHP: AppService_admin::release()
   * 逻辑类型: undefined - undefined
   */
  async release(id: number) {
    // 基于PHP真实逻辑: release
    // PHP原文: $version = $this->model->where([['id', '=', $id],['site_id', '=', $this->site_id]])->findOrEmpty();         if ($version->isEmpty()) throw new CommonE...
const version = this.model.where([['id', '=', id],['site_id', '=', this.site_id))).findOrEmpty(];
        if (version.isEmpty()) throw new BusinessException("版本不存在");
        if (version.status != AppDict.STATUS_UPLOAD_SUCCESS) throw new BusinessException("版本未上传成功");

        this.model.update([release_time: time(), status: AppDict.STATUS_PUBLISHED], [id: id]];
    }

    async generateSingCert(data) {
        return this.coreAppCloudService.generateSingCert(data);
    }
}
  }

  /**
   * generateSingCert
   * 对应 PHP: AppService_admin::generateSingCert()
   * 逻辑类型: undefined - undefined
   */
  async generateSingCert(data: any) {
    // 基于PHP真实逻辑: generateSingCert
    // PHP原文: return (new CoreAppCloudService())->generateSingCert($data);     } }...
return this.coreAppCloudService.generateSingCert(data);
    }
}
  }
}
